package com.guo.xiao.bing.controller;

import com.github.pagehelper.PageInfo;
import com.guo.xiao.bing.entity.User;
import com.guo.xiao.bing.entity.UserExample;
import com.guo.xiao.bing.service.UserService;
import com.guo.xiao.bing.util.Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String loginValid(HttpSession session, Model model, @ModelAttribute User user) {
        List<User> list = new ArrayList();
        UserExample record  = new UserExample();
        UserExample.Criteria criteria = record.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        list = userService.findUsersByExample(record);

        if (list.size() == 0) {
            //用户名错误
            model.addAttribute("status", 1);
            return "login";
        } else {
            criteria.andPasswordEqualTo(Encryption.MD5(user.getPassword()));

            list = userService.findUsersByExample(record);
            if (list.size() == 0) {
                //密码错误
                model.addAttribute("status", 2);
                return "login";
            }
            User u = list.get(0);
            session.setAttribute("user", u);
            model.addAttribute("status", 0);
        }

        return "redirect:/user/1";
    }

    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/user/1";
    }

    @RequestMapping(value = "register", method = RequestMethod.GET)
    public String register() {
        return "register";
    }

    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String update(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        User u = userService.findUserById(user.getId());
        model.addAttribute("user", u);
        return "register";
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public String registerValid(HttpSession session, Model model, @ModelAttribute User user) {
        List<User> list = new ArrayList();
        UserExample record  = new UserExample();
        UserExample.Criteria criteria = record.createCriteria();
        if (user.getUsername() == null) {
            // 用户名不能为空
            model.addAttribute("status", 3);
            return "register";
        }
        criteria.andUsernameEqualTo(user.getUsername());
        list = userService.findUsersByExample(record);

        if (list.size() == 0) {
            user.setPassword(Encryption.MD5(user.getPassword()));
//            StringBuilder sb = new StringBuilder("https://www.gravatar.com/avatar/");
//            sb.append(user.getPassword());
//            sb.append("?d=identicon");
//            user.setPic(sb.toString());

            int integer = 0;
            if (user.getId() != null) {
                integer = userService.updateUser(user);
            } else {
                integer = userService.saveUser(user);
            }

            if (integer > 0) {
                list = userService.findUsersByExample(record);
                if (list.size() > 0) {
                    session.setAttribute("user", list.get(0));
                    model.addAttribute("status", 0);
                } else {
                    //保存失败
                    model.addAttribute("status", 2);
                    return "register";
                }

            } else {
                //保存失败
                model.addAttribute("status", 2);
                return "register";
            }

        } else {
            User u = (User) session.getAttribute("user");
            if (u != null) {
                if (u.getUsername().equals(list.get(0).getUsername())) {
                    userService.updateUser(user);
                    return "redirect:/user/1";
                }

            }
            //用户名已存在
            model.addAttribute("status", 1);
            return "register";
        }
        return "redirect:/user/1";
    }

    @RequestMapping(value = "/{page}", method = RequestMethod.GET)
    public String pageUser(@PathVariable Integer page, Model model, @ModelAttribute User user) {
        PageInfo<User> userPageInfo = userService.pageFindUsersByExample(page, user);
        model.addAttribute("page", userPageInfo);
        return "user/list";
    }

    @RequestMapping(value = "del/{id}", method = RequestMethod.GET)
    public String del(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        if ("admin" .equals(user.getUsername())) {
            userService.delete(id);
        }
        return "redirect:/user/1";
    }
}
